//
//  WHLabel.swift
//  SwiftReadSail
//
//  Created by 方世沛 on 6/7/18.
//  Copyright © 2018 方世沛. All rights reserved.
//

import UIKit

class WHLabel: UILabel {

//    public var edgInsets = UIEdgeInsets.zero as UIEdgeInsets {
//        didSet {
//            self.setNeedsDisplay()
//        }
//    }
//
////  override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
////    let rect = super .textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
////    return CGRect.init(x: self.edgInsets.left, y: self.edgInsets.top, width: (rect.size.width - self.edgInsets.bottom - self.edgInsets.top), height: (rect.size.height - self.edgInsets.right - self.edgInsets.left))
////  }
//
//  override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
//    let rect = super .textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
//    return CGRect.init(x: self.edgInsets.left, y: self.edgInsets.top, width: (rect.size.width - self.edgInsets.right - self.edgInsets.left), height: (rect.size.height - self.edgInsets.top - self.edgInsets.bottom))
//  }
//
//    override func drawText(in rect: CGRect) {
//        super.drawText(in: UIEdgeInsetsInsetRect(rect, self.edgInsets))
//    }
  
  private var padding = UIEdgeInsets.zero
  
  @IBInspectable
  var paddingLeft: CGFloat {
    get { return padding.left }
    set { padding.left = newValue }
  }
  
  @IBInspectable
  var paddingRight: CGFloat {
    get { return padding.right }
    set { padding.right = newValue }
  }
  
  @IBInspectable
  var paddingTop: CGFloat {
    get { return padding.top }
    set { padding.top = newValue }
  }
  
  @IBInspectable
  var paddingBottom: CGFloat {
    get { return padding.bottom }
    set { padding.bottom = newValue }
  }
  
  
  // 文字区域
  override func drawText(in rect: CGRect) {
      super.drawText(in: rect.inset(by: padding))
    
  }
  
  // UILabel的内容区域
  override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect{
    let insets = self.padding
      var rect = super .textRect(forBounds: bounds.inset(by: insets), limitedToNumberOfLines: numberOfLines)
    //    return CGRect.init(x: self.edgInsets.left, y: self.edgInsets.top, width: (rect.size.width - self.edgInsets.right - self.edgInsets.left), height: (rect.size.height - self.edgInsets.top - self.edgInsets.bottom))
//    var rect = super.textRectForBounds(UIEdgeInsetsInsetRect(bounds, insets), limitedToNumberOfLines: numberOfLines)
    rect.origin.x    -= insets.left
    rect.origin.y    -= insets.top
    rect.size.width  += (insets.left + insets.right)
    rect.size.height += (insets.top + insets.bottom)
    return rect
  }
  
}
